import { defineStore } from "pinia";
import router from "@/router";
import { backToHome } from "@/utils";
import { useUserStore } from "@/store/modules/user"

const loadingDefaultTitle = "系统处理中，请稍后 ~"

interface ILoading{
    show:boolean,
    title:string
}

interface ITime{
    /** 右上角显示时间 */
    show:number,
    /** 路由链接 */
    routerPath:string,
    
}

interface IState {
    loading:ILoading,
    time:ITime,
    /** 是否空闲 */
    idle:boolean,
    /** 全局时间，一秒触发一次，监听它来实现定时器 */
    globalTime:number
}

// 对外部暴露一个 use 方法，该方法会导出我们定义的 state
export const useVariableStore = defineStore({
    // 每个 store 的 id 必须唯一
    id: 'variable',
    // state 表示数据源
    state: (): IState => ({
        // 全局loading加载
        loading: {
            show:false,
            title:loadingDefaultTitle
        },
        // 右上角时间
        time:{
            show:0,
            routerPath:""
        },
        idle:false,
        globalTime:0
    }),
    // getters 类似于 computed，可对 state 的值进行二次计算
    getters: {
        
    },
    // actions 用来修改 state
    actions: {
        /** 显示全局loading */
        showLoading(title?:string){
            this.$patch((state) => {
                state.idle = false
                state.loading = {
                    show:true,
                    title:title ? title : loadingDefaultTitle
                }
            })
        },
        setGlobalTime(){
            this.$patch((state) => {
                if(state.globalTime >= 1000){
                    state.globalTime = 0
                }else{
                    state.globalTime += 1
                }
            })
        },
        /** 关闭全局loading */
        closeLoading(){
            this.$patch((state) => {
                state.loading = {
                    show:false,
                    title:loadingDefaultTitle
                }
            })
        },
        /** 清除右上角时间 */
        clearTime(){
            this.$patch((state) => {
                state.time = {
                    show:0,
                    routerPath:""
                }
            })
        },
        /** 重置当前页倒计时 */
        resetTime(p_time?:number){
            const route = router.currentRoute
            if(route.value.meta && route.value.meta.time){
                this.$patch((state) => {
                    let time=route.value.meta.time as number;
                    if(typeof p_time !=='undefined'){
                      time=p_time;
                    }
                    state.time.show = time;
                })
            }
        },
        /** 刷新右上角时间 */
        setTime(){
            const route = router.currentRoute
            const userStore = useUserStore()
            if(route.value.meta && route.value.meta.time){
                if(this.time.routerPath && route.value.path === this.time.routerPath && this.time.show){
                    // 倒计时
                    this.time.show = this.time.show - 1
                    if(this.time.show === 0 && userStore.user){
                        backToHome()
                    }else if(this.time.show==0 &&  !userStore.user && route.value.path!=="/HomePage"){
                        backToHome()
                    }

                }else{
                    // 第一次赋值
                    this.$patch((state) => {
                        state.time = {
                            show:route.value.meta.time as number,
                            routerPath:route.value.path
                        }
                    })
                }
            }
        }
    },

})